import stat

from django.shortcuts import render
from rest_framework.decorators import action
from rest_framework.views import APIView
from rest_framework import viewsets, status
from rest_framework.response import Response
from erp_system.models import MenuModel
from django.conf import settings
# Create your views here.
from plc_ai_backend.apps.erp_system.serializer.menu_serializer import MenuSerializer


class MenuView(viewsets.ModelViewSet):
    queryset = MenuModel.objects.filter(delete_flag=0).all()
    serializer_class = MenuSerializer

    def get_queryset(self):
        """
        1.所有
        2.pid
        """
        query_param = self.request.query_params("pid", None)
        if query_param:
            p_id = int(query_param)
            if p_id == 0:  # 顶级菜单
                return MenuModel.objects.filter(delete__isnull=True, delete_flag=0).all()
            else:
                return MenuModel.objects.filter(parent__id=p_id, delete_flag=0).all()
        else:
            queryset = MenuModel.objects.filter(delete_flag=0).all()

    def destroy(self, request, *args, **kwargs):
        """
        删除就是修改为1
        """
        menu = self.get_object()
        menu.delete_flag = "1"
        MenuModel.objects.filter(parent__id=menu.id).update(delete_flag=1)
        return Response(status=status.HTTP_204_NO_CONTENT)

    @action(methods=["delete"], detail=False)
    def multiple_delete(self, request, *args, **kwargs):
        ids = request.data.get("ids", None)
        if not ids:
            pass
        elif not isinstance(ids, list):
            pass
        pass


class Hello(APIView):

    def get(self, request):
        ls = []
        res = {"code": 1111, "data": ls}
        return Response(res)

#
# from django.conf import settings
#
# # 假设你的设置在settings.py中定义为MY_SETTING
# my_setting = getattr(settings, 'MY_SETTING', 'default_value')